﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ViewableRange : MonoBehaviour
{
    public static ViewableRange Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<ViewableRange>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("ViewableRange");
                _Instance = obj.AddComponent<ViewableRange>();
            }
            return _Instance;
        }
    }
    private static ViewableRange _Instance;

    public GameObject target;
    private float radius;
    private GameObject trianglePrefab;
    private GameObject circlePrefab;
    private GameObject circleObj;
    private GameObject[] triangleObj;
    private bool isAwake;
    const int part = 180;
    private float[] deepth = new float[part];
    private float deltaTime;

    void Awake()
    {
        isAwake = false;
        trianglePrefab = Resources.Load("Prefabs/ViewableRangeTriangle") as GameObject;
        circlePrefab = Resources.Load("Prefabs/Circle") as GameObject;
        triangleObj = new GameObject[part];
        for (int i = 0; i < part; i++)
        {
            Quaternion rot = Quaternion.Euler(0, 0, 360f / part * (i + 0.5f));
            triangleObj[i] = GameObject.Instantiate(trianglePrefab, transform.position, rot, transform);
            var v = triangleObj[i].GetComponent<ViewableRangeTriangle>();
            v.radius = 0;
            v.part = part;
            v.angle = 360f / part * (i + 0.5f);
            triangleObj[i].SetActive(false);
        }
        circleObj = GameObject.Instantiate(circlePrefab, transform.position, Quaternion.identity, transform);
        circleObj.transform.localScale = new Vector3(0, 0, 1);
        circleObj.SetActive(false);
    }

    void Update()
    {
        if (target == null && isAwake)
        {
            isAwake = false;
            for (int i = 0; i < part; i++)
                triangleObj[i].SetActive(false);
            circleObj.SetActive(false);
        }

        if (target != null && !isAwake)
        {
            isAwake = true;
            for (int i = 0; i < part; i++)
            {
                triangleObj[i].SetActive(true);
                var v = triangleObj[i].GetComponent<ViewableRangeTriangle>();
                v.radius = radius * ConstOfMap.xGap;
            }
            circleObj.SetActive(true);
            circleObj.transform.localScale = new Vector3(radius * 2 * ConstOfMap.xGap, radius * 2 * ConstOfMap.xGap, 1);

        }

        if (target != null)
        {
            if (target.GetComponent<CanvasRenderer>() == null)
                transform.position = target.transform.position;
            else
                transform.position = Camera.main.ScreenToWorldPoint(target.transform.position);
        }
    }

    public void ChangeTarget(GameObject target)
    {
        if (target == null)
        {
            this.target = target;
            return;
        }
        this.target = target;
        BattleObject bt = target.GetComponent<BattleObject>();
        if (bt != null)
            radius = bt.searchRange;
        else
            radius = 0;
        if (isAwake)
        {
            for (int i = 0; i < part; i++)
            {
                var v = triangleObj[i].GetComponent<ViewableRangeTriangle>();
                v.radius = radius * ConstOfMap.xGap;
            }
            circleObj.transform.localScale = new Vector3(radius * 2 * ConstOfMap.xGap, radius * 2 * ConstOfMap.xGap, 1);
        }
    }

    public void ChangeTarget(GameObject target, float radius)
    {
        if (target == null)
        {
            this.target = target;
            return;
        }
        if (target == this.target && radius == this.radius)
            return;
        this.target = target;
        if (isAwake)
        {
            for (int i = 0; i < part; i++)
            {
                var v = triangleObj[i].GetComponent<ViewableRangeTriangle>();
                v.radius = radius * ConstOfMap.xGap;
            }
            circleObj.transform.localScale = new Vector3(radius * 2 * ConstOfMap.xGap, radius * 2 * ConstOfMap.xGap, 1);
        }
    }
}

